home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 323_01 / expgen.c < prev    next >
Text File  |  1990-08-04  |  15KB  |  513 lines

  1. /****************************************************************************
  2. * EXPGEN.C - Generates explosion data files for EXPLOD.C
  3. *
  4. * This program should be compilable with most C compilers.
  5. * To compile:
  6. *       Datalight C:   dlc expgen.c
  7. *       Turbo C:       tcc expgen.c
  8. *
  9. * The contents of this file is in the public domain.
  10. *
  11. * Change Log
  12. * 90/01/20 Dennis Lo         Verson 1.0 
  13. ****************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include "explod.h"
  18.  
  19. /*
  20.  * File format:
  21.  *  <num_points>
  22.  *  <num_frames>
  23.  *  <gravity>
  24.  *  <wind>
  25.  *  <trail length>
  26.  *  <x> <y>
  27.  *  <x> <y>
  28.  *  <x> <y>
  29.  *   ...
  30.  */
  31.  
  32. /* explosion parameters */
  33. int Num_points = 250;
  34. int Num_frames = 50;
  35. int Xsize = 200;
  36. int Ysize = 120;
  37. int Par1 = -1;
  38. int Par2 = -1;
  39. int Par3 = -1;
  40. int Gravity = 9;
  41. int Wind = 0;
  42. int Trail_length = 5;
  43. char Shape = '\0';
  44.  
  45. /*=====================================================================
  46.  * main
  47.  *=====================================================================
  48.  */
  49. main(argc, argv)
  50.     int argc;
  51.     char **argv;
  52. {
  53.     GetArgs(argc, argv);
  54.     srand (Par1 + Par2 + Xsize + Ysize + Num_points + Num_frames 
  55.         + Gravity + Wind + Trail_length);
  56.     switch (Shape)
  57.     {
  58.     case 'r':
  59.         PrintHeader();
  60.         if (Par1 == -1) Par1 = 0;  /* default no clumping */
  61.         if (Par2 == -1) Par2 = 8;  /* default clump size */
  62.         if (Par3 == -1) Par3 = 16; /* default clump spread size */
  63.         OvalGen (Num_points, Xsize, Ysize, Par1, Par2, Par3);
  64.         break;
  65.     case 'x':
  66.         PrintHeader();
  67.         DStarGen (Num_points, Xsize, Ysize, Par2);
  68.         break;
  69.     case '+':
  70.         PrintHeader();
  71.         HStarGen (Num_points, Xsize, Ysize, Par1);
  72.         break;
  73.     case 't':
  74.         PrintHeader();
  75.         TriangGen (Num_points, Xsize, Ysize, Par1, Par2);
  76.         break;
  77.     case 's':
  78.         PrintHeader();
  79.         TStarGen (Num_points, Xsize, Ysize, Par1, Par2);
  80.         break;
  81.     default:
  82.         printf ("No valid shape specified\n", Shape);
  83.         Instructions();
  84.         exit (1);
  85.     }
  86. }
  87.  
  88. /*=====================================================================
  89.  * Output the file header
  90.  *=====================================================================
  91.  */
  92. PrintHeader ()
  93. {
  94.     printf ("# EXPLOD DATA FILE\n");
  95.     printf ("%3d  # number of points\n", Num_points);
  96.     printf ("%3d  # number of frames\n", Num_frames);
  97.     printf ("%3d  # gravity\n", Gravity);
  98.     printf ("%3d  # wind\n", Wind);
  99.     printf ("%3d  # trail length\n", Trail_length);
  100. }
  101.  
  102. /*=====================================================================
  103.  * Process command line arguments.
  104.  *=====================================================================
  105.  */
  106. GetArgs (argc, argv)
  107.     int argc;
  108.     char *argv[];
  109. {
  110.     /*
  111.      * Loop to parse each command line parameter
  112.      */
  113.     while (--argc > 0) 
  114.     {
  115.         if (**++argv == '-') 
  116.         {
  117.             switch ((*argv)[1])
  118.             {
  119.         case 'a':        /* -a: Par 1 */
  120.             Par1 = atoi ((*argv) + 2);
  121.             break;
  122.         case 'b':        /* -b: Par 2 */
  123.             Par2 = atoi ((*argv) + 2);
  124.             break;
  125.         case 'c':        /* -c: Par 3 */
  126.             Par3 = atoi ((*argv) + 2);
  127.             break;
  128.                 case 'g':               /* -g: gravity (vert accel) */
  129.                     Gravity = atoi ((*argv) + 2);
  130.                     break;
  131.                 case 'w':               /* -w: wind (horiz accel) */
  132.                     Wind = atoi ((*argv) + 2);
  133.                     break;
  134.                 case 'x':               /* -x: explosion X size */
  135.                     Xsize = atoi ((*argv) + 2);
  136.                     break;
  137.                 case 'y':               /* -y: explosion Y size */
  138.             Ysize = atoi ((*argv) + 2);
  139.                     break;
  140.                 case 'p':               /* -p: # of explosion points */
  141.                     Num_points = atoi ((*argv) + 2);
  142.             if (Num_points > MAX_POINTS)
  143.             {
  144.             printf ("Maximum # of points is %d\n", MAX_POINTS);
  145.             exit (1);
  146.             }
  147.                     break;
  148.                 case 'f':               /* -f: # of frames */
  149.                     Num_frames = atoi ((*argv) + 2);
  150.             if (Num_frames > MAX_FRAMES)
  151.             {
  152.             printf ("Maximum # of frames is %d\n", MAX_FRAMES);
  153.             exit (1);
  154.             }
  155.                     break;
  156.                 case 't':               /* -t: trail length */
  157.                     Trail_length = atoi ((*argv) + 2);
  158.                     break;
  159.                 case 's':               /* -s[rsd]: shape */
  160.                     Shape = (*argv)[2];
  161.                     break;
  162.                 default:
  163.                     printf ("*** Invalid option: %s\n", *argv);
  164.                     Instructions();
  165.                     exit (1);
  166.             }
  167.         }
  168.     }
  169. }
  170.  
  171. /*=====================================================================
  172.  * Print instructions
  173.  *=====================================================================
  174.  */
  175. Instructions ()
  176. {
  177.     puts ("Usage: expgen [parameters] >output_file");
  178.     puts ("Parameters can be one of");
  179.     puts ("  -sC   :C = Explosion shape: r (round), + (star1), x (star2),");
  180.     puts ("             t (triangle), or s (star transformation). No default.");
  181.     puts ("  -xN   :N = Explosion X size.  Default: 200");
  182.     puts ("  -yN   :N = Explosion Y size.  Default: 120");
  183.     puts ("  -pN   :N = No. of points (max 300) Default: 250");
  184.     puts ("  -fN   :N = No. of frames (max 60) Default: 50");
  185.     puts ("  -tN   :N = Trail length.      Default: 5");
  186.     puts ("  -gN   :N = Gravity.           Default: 9");
  187.     puts ("  -wN   :N = Wind.              Default: 0");
  188.     puts ("  -aN   :N = Extra parameter 1.  Affects each shape differently.");
  189.     puts ("  -bN   :N = Extra parameter 2.  Affects each shape differently.");
  190.     puts ("  -cN   :N = Extra parameter 3.  Affects each shape differently.");
  191. }
  192.  
  193. /*====================================================================
  194.  * Return a random number between 1..maxval
  195.  *====================================================================
  196.  */
  197. int
  198. rnd (maxval)
  199. int maxval;
  200. {
  201. #   define MAX_RAND 32767       /* max value returned by rand() */
  202.     long l;
  203.  
  204.     l = (long) maxval * rand() / MAX_RAND;
  205.     return ((int) l);
  206. }
  207.  
  208. /*====================================================================
  209.  * Calculate/write the src & dest points for an oval explosion.
  210.  *====================================================================
  211.  */
  212. OvalGen (num_points, xsize, ysize, par1, par2, par3)
  213.     int num_points;
  214.     int xsize, ysize;
  215.     int par1;  /* 1 = clump */
  216.     int par2;  /* clump size */
  217.     int par3;  /* clump spread range */
  218. {
  219.     int dest_x, dest_y;
  220.     int src_x, src_y;
  221.     long x2 = (long) xsize * xsize;
  222.     long y2 = (long) ysize * ysize;
  223.     long x2y2 = (long) xsize * xsize * ysize * ysize;
  224.     int i = 0;
  225.     int j;
  226.     int par3div2 = par3 >> 1;
  227.     int par3div4 = par3 >> 2;
  228.  
  229.     src_x = 0;
  230.     src_y = 0;
  231.     for (i=0; i<num_points; i++) 
  232.     {
  233.         /* 
  234.          * Find a dest coordinates that is inside the ellipse with
  235.          * X radius of xsize and Y radius of ysize. */
  236.         while (1)
  237.         {
  238.             dest_x = rnd (2*xsize) - xsize;
  239.             dest_y = rnd (2*ysize) - ysize;
  240.  
  241.         /* Accept the point if it is within the oval */
  242.         if (y2 * dest_x * dest_x  +  x2 * dest_y * dest_y  <  x2y2)
  243.         break;
  244.         }
  245.     printf ("%d %d  %d %d\n", src_x, src_y, dest_x, dest_y);
  246.  
  247.      /* If -a1 then do clumping */
  248.     if (par1 == 1)
  249.     {
  250.         for (j=0; j<par2; j++)
  251.         printf ("%d %d  %d %d\n", src_x, src_y, 
  252.             dest_x + rnd(par3) - par3div2, 
  253.             dest_y + rnd(par3div2) - par3div4);
  254.         i += par2;
  255.     }
  256.     }
  257. }
  258.  
  259. /*====================================================================
  260.  * Calculate/write the src & dest points for a diagonal star explosion.
  261.  *====================================================================
  262.  */
  263. DStarGen (num_points, xsize, ysize, par1)
  264.     int num_points;
  265.     int xsize, ysize;
  266.     int par1;
  267. {
  268.     int dest_x, dest_y;
  269.     int src_x, src